'use strict';

/**
 * Config for the router
 */
app.config(['$stateProvider', '$urlRouterProvider', '$controllerProvider', '$compileProvider', '$filterProvider', '$provide', '$ocLazyLoadProvider', 'JS_REQUIRES',
    function($stateProvider, $urlRouterProvider, $controllerProvider, $compileProvider, $filterProvider, $provide, $ocLazyLoadProvider, jsRequires) {

        app.controller = $controllerProvider.register;
        app.directive = $compileProvider.directive;
        app.filter = $filterProvider.register;
        app.factory = $provide.factory;
        app.service = $provide.service;
        app.constant = $provide.constant;
        app.value = $provide.value;

        // LAZY MODULES

        $ocLazyLoadProvider.config({
            debug: false,
            events: true,
            modules: jsRequires.modules
        });

        // APPLICATION ROUTES
        // -----------------------------------
        // For any unmatched url, redirect to /app/dashboard
        $urlRouterProvider.otherwise("/app/report/list");

        // 登录
        $stateProvider.state('login', {
            url: "/login",
            templateUrl: "assets/views/login/login.html",
            resolve: loadSequence('utils', 'loginCtrl'),
        })

        // Set up the states
        .state('app', {
            url: "/app",
            templateUrl: "assets/views/app.html",
            resolve: loadSequence('modernizr', 'moment', 'angularMoment', 'uiSwitch', 'perfect-scrollbar-plugin', 'toaster', 'ngAside', 'vAccordion', 'sweet-alert', 'chartjs', 'tc.chartjs', 'oitozero.ngSweetAlert', 'utils','ngLocale','mainContentCtrl'),
            abstract: true
        })
        .state('app.cooks', {
            url: "/cooks",
            templateUrl: "assets/views/ui-view.html",
            // resolve: loadSequence(),
            abstract: true
        }).state('app.cooks.list', {
            url: "/list",
            templateUrl: "assets/views/cooks/cooklist.html",
            resolve: loadSequence('cookListCtrl', 'ngTable'),
            title: '厨师管理'
        }).state('app.cooks.add', {
            url: "/add",
            templateUrl: "assets/views/cooks/addCook.html",
            resolve: loadSequence('addCookCtrl', 'angularFileUpload'),
            title: '添加厨师'
        }).state('app.cooks.edit', {
            url: "/edit",
            templateUrl: "assets/views/cooks/addCook.html",
            resolve: loadSequence('addCookCtrl', 'angularFileUpload'),
            title: '编辑厨师'
        }).state('app.cooks.detail', {
            url: "/detail",
            templateUrl: "assets/views/cooks/cookDetail.html",
            resolve: loadSequence('cookDetailCtrl'),
            title: '厨师详情'
        }).state('app.modifyPasswd', {
            url: "/modifyPasswd",
            templateUrl: "assets/views/modifyPasswd.html",
            resolve: loadSequence('modifyPasswdCtrl'),
            title: '修改密码'
        }).state('app.resources', {
            url: "/resources",
            templateUrl: "assets/views/ui-view.html",
            // resolve: loadSequence(),
            abstract: true
        }).state('app.resources.download', {
            url: "/download",
            templateUrl: "assets/views/resourceDownload.html",
            resolve: loadSequence('resourceDownloadCtrl'),
            title: '表格下载'
        })

        // -----------------------start 管理员 --------------------------
        .state('app.administrator', {
            url: "/administrator",
            templateUrl: "assets/views/ui-view.html",
            abstract: true
        })
        .state('app.administrator.list', {
            url: "/list",
            templateUrl: "assets/views/administrator/administrator.html",
            resolve: loadSequence('utils', 'administratorCtrl', 'ngTable'),
            title: '管理员列表'
        })
        .state('app.administrator.create', {
            url: "/create",
            templateUrl: "assets/views/administrator/administrator_create.html",
            resolve: loadSequence('utils', 'administratorCreateCtrl'),
            title: '管理员添加'
        })
        .state('app.administrator.edit', {
            url: "/edit",
            templateUrl: "assets/views/administrator/administrator_edit.html",
            resolve: loadSequence('utils', 'administratorEditCtrl'),
            title: '管理员编辑'
        })
        // -----------------------end 管理员 --------------------------

        // -----------------------start 协管员 --------------------------
        .state('app.warden', {
            url: "/warden",
            templateUrl: "assets/views/ui-view.html",
            abstract: true
        })
        .state('app.warden.list', {
            url: "/list",
            templateUrl: "assets/views/warden/warden.html",
            resolve: loadSequence('utils', 'wardenCtrl', 'ngTable'),
            title: '协管员列表'
        })
        .state('app.warden.create', {
            url: "/create",
            templateUrl: "assets/views/warden/warden_create.html",
            resolve: loadSequence('utils', 'wardenCreateCtrl'),
            title: '协管员添加'
        })
        .state('app.warden.edit', {
            url: "/edit",
            templateUrl: "assets/views/warden/warden_edit.html",
            resolve: loadSequence('utils', 'wardenEditCtrl'),
            title: '协管员编辑'
        })
        // -----------------------end 协管员 --------------------------

        // -----------------------start 群宴报备 --------------------------
        .state('app.report', {
            url: "/report",
            templateUrl: "assets/views/ui-view.html",
            abstract: true
        })
        .state('app.report.list', {
            url: "/list",
            templateUrl: "assets/views/report/report.html",
            resolve: loadSequence('utils', 'reportCtrl', 'ngTable'),
            title: '群宴报备'
        })
        .state('app.report.info', {
            url: "/info",
            templateUrl: "assets/views/report/report_info.html",
            resolve: loadSequence('utils','reportInfoCtrl'),
            title: '群宴报备详情'
        })
        .state('app.report.record', {
            url: "/record",
            templateUrl: "assets/views/report/report_record.html",
            resolve: loadSequence('utils','reportRecordCtrl'),
            title: '群宴报检查记录'
        })
        .state('app.report.recordDetail', {
            url: "/recordDetail",
            templateUrl: "assets/views/report/report_recordDetail.html",
            resolve: loadSequence('utils','reportRecordDetailCtrl'),
            title: '群宴报检查记录'
        })
        // -----------------------end 群宴报备 --------------------------


        // -----------------------start 统计报表 --------------------------
        .state('app.statistics', {
            url: "/statistics",
            templateUrl: "assets/views/ui-view.html",
            abstract: true
        }).state('app.statistics.count', {
            url: "/count",
            templateUrl: "assets/views/count/count.html",
            resolve: loadSequence('highcharts','utils', 'countCtrl'),
            title: '统计报表'
        })
        .state('app.statistics.detail', {
            url: "/detail",
            templateUrl: "assets/views/count/count_detail.html",
            resolve: loadSequence('utils', 'countDetailCtrl'),
            title: '统计明细'
        })
        // -----------------------end 统计报表 --------------------------

        // Generates a resolve object previously configured in constant.JS_REQUIRES (config.constant.js)
        function loadSequence() {
            var _args = arguments;
            return {
                deps: ['$ocLazyLoad', '$q',
                    function($ocLL, $q) {
                        var promise = $q.when(1);
                        for (var i = 0, len = _args.length; i < len; i++) {
                            promise = promiseThen(_args[i]);
                        }
                        return promise;

                        function promiseThen(_arg) {
                            if (typeof _arg == 'function')
                                return promise.then(_arg);
                            else
                                return promise.then(function() {
                                    var nowLoad = requiredData(_arg);
                                    if (!nowLoad)
                                        return $.error('Route resolve: Bad resource name [' + _arg + ']');
                                    return $ocLL.load(nowLoad);
                                });
                        }

                        function requiredData(name) {
                            if (jsRequires.modules)
                                for (var m in jsRequires.modules)
                                    if (jsRequires.modules[m].name && jsRequires.modules[m].name === name)
                                        return jsRequires.modules[m];
                            return jsRequires.scripts && jsRequires.scripts[name];
                        }
                    }
                ]
            };
        }
    }
]);
